import { _decorator, Component, Node, sp } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('SpineEffectController')
export class SpineEffectController extends Component {

    @property([String])
    private spines: Array<string> = [];
    @property(Node)
    private animParent: Node = null;

    private curIndex: number = 0;
    private spineAnim: sp.Skeleton;

    public completeListener: any;

    onLoad() {
        this.spineAnim = this.node.getComponent(sp.Skeleton);
    }

    start() {
        this.PlaySpineAnim(this.spines[this.curIndex]);
    }

    PlaySpineAnim(animName) {
        this.spineAnim.setSkin(animName);
        this.spineAnim.setAnimation(0, animName, false);
        this.spineAnim.setCompleteListener((trackEntry) => {
            if (this.curIndex >= this.spines.length) {
                if (this.animParent) {
                    this.animParent.destroy();
                    return;
                }
                this.node.destroy();
            }
            this.curIndex++;
            this.PlaySpineAnim(this.spines[this.curIndex]);
        });
    }

}


